package org.tc.feign.product.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tc.feign.service.FeignJoinComService;
import org.tc.product.entity.Joincommoditys;
import org.tc.product.util.PageHelper;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static org.tc.feign.shopmall.controller.DateTest.formatDate;
import static org.tc.feign.shopmall.controller.DateTest.getCurrYearFirst;
import static org.tc.feign.shopmall.controller.DateTest.getCurrYearLast;

@CrossOrigin(origins = {"http://localhost:8080"}, methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.OPTIONS}, allowCredentials = "true")
@RestController
public class JoinCommodityComtroller {

    @Autowired
    private FeignJoinComService feignJoinComService;

    @RequestMapping("/insertJoin")
    public int insertJoin(@RequestBody Joincommoditys joincommoditys) {
        int count = feignJoinComService.insertJoin(joincommoditys);
        return count;
    }

    @RequestMapping("/selectAllJoin")
    public PageHelper<Joincommoditys> selectAllJoin(int pageNo, int pageSize, Integer storeid) {
        return feignJoinComService.selectAllJoin(pageNo, pageSize, storeid);
    }

    /**
     * 获取某个店面当月的进货内容
     *
     * @param storeid 分店ID
     * @return
     */
    @RequestMapping("/QueryMonthJoin")
    public List<Joincommoditys> QueryMonthJoin(Integer storeid) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        //获取当前月第一天：
        Calendar calstr = Calendar.getInstance();
        //本月
        calstr.add(Calendar.MONTH, 0);
        //设置为1号为本月第一天
        calstr.set(Calendar.DAY_OF_MONTH, 1);
        String starttime = df.format(calstr.getTime());
        String jointime = df.format(new Date());// new Date()为获取当前系统时间
        System.out.println(starttime + "----" + jointime);
        List<Joincommoditys> list = feignJoinComService.QueryMonthJoon(starttime, jointime, storeid);
        return list;
    }

    /**
     * 获取某个店面本周的进货内容
     *
     * @param storeid 分店ID
     * @return
     */
    @RequestMapping("/QueryWeekJoin")
    @ResponseBody
    public List<Joincommoditys> QueryWeekJoin(Integer storeid) {
        String starttime, jointime, curWeek;
        Calendar curStartCal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
        SimpleDateFormat dfWeek = new SimpleDateFormat("(第ww周)");
        Calendar cal = (Calendar) curStartCal.clone();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        starttime = df.format(cal.getTime());
        curWeek = dfWeek.format(cal.getTime());
        cal.add(Calendar.DATE, 6);
        jointime = df.format(cal.getTime());
        System.out.println("本周周一和周末:" + starttime + " and " + jointime + " and " + curWeek);
        List<Joincommoditys> list = feignJoinComService.QueryMonthJoon(starttime, jointime, storeid);
        return list;
    }

    /**
     * 获取某个店面本年度的进货内容
     *
     * @param storeid 分店ID
     * @return
     */
    @RequestMapping("/QueryYearJoin")
    @ResponseBody
    public List<Joincommoditys> QueryYearJoin(Integer storeid) {
        String starttime = formatDate(getCurrYearFirst());
        String jointime = formatDate(getCurrYearLast());
        System.out.println(formatDate(getCurrYearFirst()));
        System.out.println(formatDate(getCurrYearLast()));
        List list = feignJoinComService.QueryMonthJoon(starttime, jointime, storeid);
        return list;
    }

    /**
     * 获取某个店面当天的进货内容
     *
     * @param storeid 分店ID
     * @return
     */
    @RequestMapping("/QueryDayJoin")
    @ResponseBody
    public List<Joincommoditys> QueryDayJoin(Integer storeid) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String jointime = df.format(new Date());// new Date()为获取当前系统时间
        System.out.println(jointime);
        List<Joincommoditys> list = feignJoinComService.QueryDayJoin(jointime, storeid);
        return list;
    }

    /**
     * 本月进货数量
     *
     * @param storeid
     * @return
     */
    @RequestMapping("/QueryMonthCountJoin")
    @ResponseBody
    public int QueryCountJoin(Integer storeid) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        //获取当前月第一天：
        Calendar calstr = Calendar.getInstance();
        //本月
        calstr.add(Calendar.MONTH, 0);
        //设置为1号为本月第一天
        calstr.set(Calendar.DAY_OF_MONTH, 1);
        String starttime = df.format(calstr.getTime());
        String jointime = df.format(new Date());// new Date()为获取当前系统时间
        System.out.println(starttime + "----" + jointime);
        int count = feignJoinComService.QueryCountJoin(starttime, jointime, storeid);
        return count;
    }

    /**
     * 本周进货数量
     *
     * @param storeid
     * @return
     */
    @RequestMapping("/QueryWeekCountJoin")
    @ResponseBody
    public int QueryWeekCountJoin(Integer storeid) {
        String starttime, jointime, curWeek;
        Calendar curStartCal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
        SimpleDateFormat dfWeek = new SimpleDateFormat("(第ww周)");
        Calendar cal = (Calendar) curStartCal.clone();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        starttime = df.format(cal.getTime());
        curWeek = dfWeek.format(cal.getTime());
        cal.add(Calendar.DATE, 6);
        jointime = df.format(cal.getTime());
        System.out.println("本周周一和周末:" + starttime + " and " + jointime + " and " + curWeek);
        int count = feignJoinComService.QueryCountJoin(starttime, jointime, storeid);
        return count;
    }

    /**
     * 本年度进货数量
     *
     * @param storeid
     * @return
     */
    @RequestMapping("/QueryYearCountJoin")
    @ResponseBody
    public int QueryYearCountJoin(Integer storeid) {
        String starttime = formatDate(getCurrYearFirst());
        String jointime = formatDate(getCurrYearLast());
        System.out.println(formatDate(getCurrYearFirst()));
        System.out.println(formatDate(getCurrYearLast()));
        int count = feignJoinComService.QueryCountJoin(starttime, jointime, storeid);
        return count;
    }

    /**
     * 当日进货数量
     *
     * @param storeid
     * @return
     */
    @RequestMapping("/QueryDayCountJoin")
    @ResponseBody
    public int QueryDayCountJoin(Integer storeid) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String jointime = df.format(new Date());// new Date()为获取当前系统时间
        System.out.println(jointime);
        int count = feignJoinComService.QueryDayCountJoin(jointime, storeid);
        return count;
    }

}
